package com.youzikj.wx.comm.oauth2;

import java.awt.List;

import org.apache.log4j.Logger;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.youzikj.wx.comm.util.CommonUtil;

public class AdvancedUtil {
	private static Logger log = Logger.getLogger(AdvancedUtil.class);
	/**
	 * 获取网页授权凭证
	 */
	public static WeixinOauth2Token getOauth2AccessToken(String appId,String appSecret,String code){
		WeixinOauth2Token wat = null;
		//拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
		requestUrl = requestUrl.replace("APPID", appId);
		requestUrl = requestUrl.replace("SECRET", appSecret);
		requestUrl = requestUrl.replace("CODE", code);
		System.out.println(requestUrl);
		//获取网页授权凭证
		JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
		log.info("=========jsonObject:"+jsonObject.toString());
		if(null != jsonObject){
			try {
				wat = new WeixinOauth2Token();
				wat.setAccessToken(jsonObject.getString("access_token"));
				wat.setExpiresIn(jsonObject.getInt("expires_in"));
				wat.setRefreshToken(jsonObject.getString("refresh_token"));
				wat.setOpenId(jsonObject.getString("openid"));
				wat.setScope(jsonObject.getString("scope"));
			} catch (Exception e) {
				wat = null;
				int errorCode = jsonObject.getInt("errcode");
				String errorMsg = jsonObject.getString("errmsg");
				log.info("获取网页授权凭证失败 errcode:{}"+errorCode+",errmsg:{}"+errorMsg);
			}
		}
		return wat;
	}
	

	/**
	 * 刷新网页授权凭证
	 */
	public static WeixinOauth2Token refreshOauth2AccessToken(String appId,String refreshToken){
		WeixinOauth2Token wat = null;
		//拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token？appid=APPID&grant_type=refresh_code&refresh_token=REFRESH_TOKEN";
		requestUrl = requestUrl.replace("APPID", appId);
		requestUrl = requestUrl.replace("REFRESH_TOKEN", refreshToken);
		//获取网页授权凭证
		JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
		if(null != jsonObject){
			try {
				wat = new WeixinOauth2Token();
				wat.setAccessToken(jsonObject.getString("access_token"));
				wat.setAccessToken(jsonObject.getString("expires_in"));
				wat.setAccessToken(jsonObject.getString("refresh_token)"));
				wat.setAccessToken(jsonObject.getString("openid"));
				wat.setAccessToken(jsonObject.getString("scope"));
			} catch (Exception e) {
				wat = null;
				int errorCode = jsonObject.getInt("errcode");
				String errorMsg = jsonObject.getString("errmsg");
				log.info("刷新网页授权凭证失败 errcode:{}"+errorCode+",errmsg:{}"+errorMsg);
			}
		}
		return wat;
		
	}
	
	/**
	 * 通过网页授权获取用户信息方法
	 */
	
	@SuppressWarnings({ "deprecation", "unchecked" })
	public static SNSUserInfo getSNSUserInfo(String accessToken,String openId){
		SNSUserInfo snsUserInfo = null;
		//拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
		String ss = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);
		System.out.println("获取用户信息地址："+ss);
		//通过网页授权获取用户信息
		JSONObject jsonObject = CommonUtil.httpsRequest(ss, "GET", null);
		if(null != jsonObject){
			try {
				snsUserInfo = new SNSUserInfo();
				snsUserInfo.setOpenId(jsonObject.getString("openid"));
				snsUserInfo.setNickname(jsonObject.getString("nickname"));
				snsUserInfo.setSex(jsonObject.getInt("sex"));
				snsUserInfo.setCountry(jsonObject.getString("country"));
				snsUserInfo.setProvince(jsonObject.getString("province"));
				snsUserInfo.setCity(jsonObject.getString("city"));
				snsUserInfo.setHeadImgUrl(jsonObject.getString("headimgurl"));
				snsUserInfo.setPrivilegeList(JSONArray.toList(jsonObject.getJSONArray("privilege"),List.class));
			} catch (Exception e) {
				snsUserInfo = null;
				int errorCode = jsonObject.getInt("errcode");
				String errorMsg = jsonObject.getString("errmsg");
				log.info("获取用户信息失败errorCode: "+errorCode+"errorMsg"+errorMsg);
			}
		}
		return snsUserInfo;
	}
	
	public static void main(String[] args) {
		WeixinOauth2Token o = getOauth2AccessToken(CommonUtil.appId, CommonUtil.appSecret, "123");
		String accessToken = o.getAccessToken();
		String openId = o.getOpenId();
		System.out.println("accessToken: "+accessToken+"openId: "+openId);
		
	}
	
	
}
